<?php
namespace app\admin\controller;
use think\View;

class Online extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
        $this->online = M('Online');
        $active['seo']['online'] = 1;
        $this->view->active = $active;
    }
    public function index()
    {
        $this->view->onlines = $this->online->select();
        return $this->view->fetch();
    }
    public function add()
    {
        if(IS_POST){
            $data['name'] = $_POST['name'];
            $data['qq'] = $_POST['qq'];
            $data['msn'] = $_POST['msn'];
            $data['aliww'] = $_POST['aliww'];
            $data['taobao'] = $_POST['taobao'];
            $data['skype'] = $_POST['skype'];
            $data['paiid'] = $_POST['paiid'];
            $data['created_at'] = time();
            $data['updated_at'] = time();


            $res = $this->online->add($data);
            if($res){
                return $this->success('添加成功', U('Online/index'));
            } else{
                return $this->error('添加失败', U('Online/index'));
            }

        }else{
            return $this->view->fetch();
        }
    }
    public function edit()
    {
        if(IS_POST){
            $where['id']= $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['qq'] = $_POST['qq'];
            $data['msn'] = $_POST['msn'];
            $data['aliww'] = $_POST['aliww'];
            $data['taobao'] = $_POST['taobao'];
            $data['skype'] = $_POST['skype'];
            $data['paiid'] = $_POST['paiid'];
            $data['created_at'] = time();
            $data['updated_at'] = time();
            $res = $this->online->where($where)->save($data);
            if($res){
                return $this->success('编辑成功', U('Online/index'));
            } else{
                return $this->error('编辑失败', U('Online/index'));
            }
        }else{
            $this->view->data = $this->online->where(['id'=>$_GET['id']])->find();
            return $this->view->fetch();
        }

    }

    public function delete()
    {
        $res = $this->online->where(['id'=>$_GET['id']])->delete();
        if($res){
            return $this->success('删除成功', U('Online/index'));
        } else{
            return $this->error('删除失败', U('Online/index'));
        }
    }
}
